﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockFinder.UI.Views;
using StockFinder.UI.Controllers;
using StockFinder.Model;
using System.Drawing;

public partial class JustBrokenOutResults : System.Web.UI.Page, IBasicResultsView
{
    private readonly JustBrokenOutResultsController _Controller;

    public JustBrokenOutResults()
    {
        _Controller = new JustBrokenOutResultsController(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //1. get the datasource from database
        RunDateCalendar.SelectedDate = DateTime.Today;

        if (!IsPostBack)
        {
            OnLoadResults();
        }
    }
    protected void ResultsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            PriceAnalysisResultContainer result = e.Item.DataItem as PriceAnalysisResultContainer;
            if (result != null)
            {
                Label SymbolLabel = e.Item.FindControl("SymbolLabel") as Label;
                Label PriceDaysLabel = e.Item.FindControl("PriceDaysLabel") as Label;
                Label BaseLengthLabel = e.Item.FindControl("BaseLengthLabel") as Label;
                Label BaseDepthLabel = e.Item.FindControl("BaseDepthLabel") as Label;
                Label BaseLowPriceLabel = e.Item.FindControl("BaseLowPriceLabel") as Label;
                Label BaseLowDateLabel = e.Item.FindControl("BaseLowDateLabel") as Label;
                Label BaseHighPriceLabel = e.Item.FindControl("BaseHighPriceLabel") as Label;
                Label BaseHighDateLabel = e.Item.FindControl("BaseHighDateLabel") as Label;
                Label CompositeLabel = e.Item.FindControl("CompositeLabel") as Label;
                Label EPSLabel = e.Item.FindControl("EPSLabel") as Label;
                Label RSLabel = e.Item.FindControl("RSLabel") as Label;
                Label SMRLabel = e.Item.FindControl("SMRLabel") as Label;
                Label GroupRSLabel = e.Item.FindControl("GroupRSLabel") as Label;
                Label AccDistLabel = e.Item.FindControl("AccDistLabel") as Label;
                System.Web.UI.WebControls.Image ShortChartImage = e.Item.FindControl("ShortChartImage") as System.Web.UI.WebControls.Image;
                System.Web.UI.WebControls.Image LongChartImage = e.Item.FindControl("LongChartImage") as System.Web.UI.WebControls.Image;
                
                SymbolLabel.Text = result.PriceAnalysisResult.SymbolName;
                PriceDaysLabel.Text = result.PriceAnalysisResult.DaysOfPrices.ToString();
                BaseLengthLabel.Text = result.PriceAnalysisResult.BaseLength.ToString();
                BaseDepthLabel.Text = result.PriceAnalysisResult.BaseDepth.ToString();
                BaseLowPriceLabel.Text = result.PriceAnalysisResult.BaseLow.ToString();
                BaseLowDateLabel.Text = result.PriceAnalysisResult.BaselowDate.ToShortDateString();
                BaseHighPriceLabel.Text = result.PriceAnalysisResult.PreviousHigh.ToString();
                BaseHighDateLabel.Text = result.PriceAnalysisResult.PreviousHighDate.ToShortDateString();
                

                if (result.IBDRatings != null)
                {
                    CompositeLabel.Text = result.IBDRatings.IBDComposite.ToString();                    
                    EPSLabel.Text = result.IBDRatings.EPS.ToString();                    
                    RSLabel.Text = result.IBDRatings.RelativeStrength.ToString();                    
                    SMRLabel.Text = string.IsNullOrEmpty(result.IBDRatings.SMR) ? string.Empty : result.IBDRatings.SMR;
                    GroupRSLabel.Text = string.IsNullOrEmpty(result.IBDRatings.GroupRSRating) ? string.Empty : result.IBDRatings.GroupRSRating;
                    AccDistLabel.Text = string.IsNullOrEmpty(result.IBDRatings.AccDist) ? string.Empty : result.IBDRatings.AccDist;

                    FormatIBDRatingNumericLabel(CompositeLabel, result.IBDRatings.IBDComposite);
                    FormatIBDRatingNumericLabel(EPSLabel, result.IBDRatings.EPS);
                    FormatIBDRatingNumericLabel(RSLabel, result.IBDRatings.RelativeStrength);                    
                }

                ShortChartImage.ImageUrl = string.Format("http://finviz.com/chart.ashx?t={0}&ty=c&ta=1&p=d&s=l", result.PriceAnalysisResult.SymbolName);
                LongChartImage.ImageUrl = string.Format("http://bigcharts.marketwatch.com/charts/big.chart?symb={0}&time=20&freq=3", result.PriceAnalysisResult.SymbolName);
            }
        }
    }

    private void FormatIBDRatingNumericLabel(Label label, decimal rating)
    {
        decimal rgbRelative = (255 / 100) * rating;
        int rgb = Convert.ToInt32(rgbRelative);

        if (rating == 50)
        {
            //yellow
            label.BackColor = Color.FromArgb(255, 255, 0);
        }
        else if (rating > 50)
        {
            //G is constant
            label.BackColor = Color.FromArgb(255 - rgb, 255, 0);
        }
        else
        {
            //rating < 50 - R is constant
            label.BackColor = Color.FromArgb(255, rgb, 0);
        }
    }

    protected void RunDateCalendar_SelectionChanged(object sender, EventArgs e)
    {
        OnLoadResults();
    }

    protected virtual void OnLoadResults()
    {
        if (LoadResults != null)
        {
            LoadResults(this, EventArgs.Empty);
        }
    }

    public void SetResults(IOrderedEnumerable<PriceAnalysisResultContainer> results)
    {
        ResultsRepeater.DataSource = results;
        ResultsRepeater.DataBind();
    }

    public DateTime GetRunDate()
    {
        return RunDateCalendar.SelectedDate;
    }

    public event EventHandler LoadResults;
}